package main

import (
	"flag"
	"github.com/gin-gonic/gin"
	"io/ioutil"
	"os"
)

/**
静态服务器
*/
var (
	dir  *string
	port *string
)

func main() {
	flag.StringVar(port, "port", "18080", "port")
	flag.StringVar(dir, "dir", "/root/download", "download dir")
	router := gin.Default()
	//data(router)
	//reader(router)
	//router.StaticFile("/", "D:\\explore\\go\\fly_snow\\")
	//router.StaticFile("/StaticServer.go", "D:\\explore\\go\\fly_snow\\web\\StaticServer.go")
	router.StaticFS("/ftp", gin.Dir(*dir, true))
	router.Run(":" + *port)
}

func reader(router *gin.Engine) {
	router.GET("/reader", func(context *gin.Context) {
		file, _ := os.Open("D:\\explore\\go\\fly_snow\\web\\StaticServer.go")
		stat, _ := os.Stat("D:\\explore\\go\\fly_snow\\web\\StaticServer.go")
		context.DataFromReader(200, stat.Size(), "text/plain; charset=utf-8", file, nil)
	})
}

func data(router *gin.Engine) {
	router.GET("/staticserver.go", func(c *gin.Context) {
		data, err := ioutil.ReadFile("D:\\explore\\go\\fly_snow\\web\\StaticServer.go")
		if err != nil {
			c.AbortWithError(500, err)
		} else {
			c.Data(200, "text/plain; charset=utf-8", data)
		}
	})
}
